#include <omp.h>
#include <stdio.h>
void flush_correct()
{
    int a, b;
    a = b = 0;
#pragma omp parallel num_threads(2)
    {
        int myid = omp_get_thread_num();
        int tmp;

        if (myid == 0) {  // thread 0
#pragma omp atomic write
            b = 1;
#pragma omp flush(a, b)  // flushes are ordered
#pragma omp atomic read
            tmp = a;
        }
        else {  // thread 1
    #pragma omp atomic write
            a = 1;
    #pragma omp flush(a, b)  // flushes are ordered
    #pragma omp atomic read
            tmp = b;
        }
        printf("Thread %d: tmp = %d\n", myid, tmp);
        
        if (tmp == 0) {  // access by single thread
            /* protected section */
        }
     
    }
}
int main() {
    flush_correct();
    return 0;
}